<?php
class User_Model extends CI_Model{
	
	public $common_model;
	public $openId;

	function __construct(){
		parent::__construct();
		$this->load->model('common_model');
		$this->common_model = &get_instance()->common_model;
		
	}
	

	/*
	 * 构造器新增 ＋ 单一记录 ＋ 获取id号
	 *
	 $data = array(
	 'id' => N
	 'title' => 'My title',
	 'name' => 'My Name',
	 'date' => 'My date'
	 );
	 */
	
	
	// 1:checked -1:format error -2:repeat ...
	function check($data){
		$checked = array("name"=>1,"phone"=>1);		
		$name = str_replace(' ', '', $data['name']);
		$phone = str_replace(' ', '', $data['phone']);
		// 检查手机号码格式
		if(!preg_match("/^13[0-9]{1}[0-9]{8}$|15[0189]{1}[0-9]{8}$|189[0-9]{8}$|17[0-9]{1}[0-9]{8}$/",$phone)){
			$checked['phone']=-1; 
		}
		// 检查是否是汉字
		if (!preg_match("/^[\x7f-\xff]+$/", $name)) { //兼容gb2312,utf-8
			$checked['name']=-1;
		}
		
		return $checked;
		
	}

	
	function add($data){
		$data['created']=date('Y-m-d H:i:s');
		$this->db->insert('j_user', $data);
		return $this->db->insert_id();
	}
	
	
	
	function update($data){
		$this->db->update('j_user', $data);
	}
	
	function delete($id){
		$this->db->delete('j_user', array('id' => $id));
	}
	
	/*  按页获取user ＋使用查询构造器查询＋获取多结果  （建议用）
	 *
	 *  $array = array('name !=' => $name, 'id <' => $id, 'date >' => $date);
	 $this->db->where($array);
	
	 // 查询条件组的用法
	 $this->db->select('*')->from('my_table')
	 ->group_start()
	 ->where('a', 'a')
	 ->or_group_start()
	 ->where('b', 'b')
	 ->where('c', 'c')
	 ->group_end()
	 ->group_end()
	 ->where('d', 'd')
	 ->get();
	 *
	 * */
	function query_page($where,$limit, $offset){
		$this->db->select();
	
		$query = $this->db->get('j_user');
		$query = $this->db->where($array);
		$query = $this->db->limit($limt,$offset);
		$result = $query->result_array();
		return $result;
	
	}
	
	public function query_one($table,$where){
		$this->db->where($where);
		$this->db->select('*');
		$query=$this->db->get($table);
		$one=$query->row_array();
		return $one;
	}
	
	
/*--------------------------------- 分 割 线 ----------------------------------------------*/	
	
	// 根据openId查询user
	function getUserByOpenId($openId){
		$where = array('openId=' => $openId);
		return $this->query_one('j_user',$where);
		
		// 显示查询语句
		//return $this->db->last_query();
	}
	
	
	
	function _get_user(){
		$openId = isset($_POST['openId'])?$_POST['openId']:(isset($_GET['openId'])?$_GET['openId']:'NULL');
		return $this->get_user($openId);
	
	}
	
	
// 	/*  根据openId获取user  */
// 	function get_user($openId){
// 		$rs = $this->db->query("select * from j_user where openId ='".$openId."'");
// 		$result = $rs->row();
// 		return $result;//这里返回的是一个数组，可以通过$query['id'],$query['title']//进行访问
// 	}
	
	
	/*  根据openId获取user + 构造器查询 ＋返回单一结果  */
	function get_user_2($openId){
		$this->db->select();
		$query = $this->db->get('j_user');
		$query = $this->db->get_where('j_user', array('openId' => $openId));
		$result = $query->row();
		return $result;//这里返回的是一个数组，可以通过$query['id'],$query['title']//进行访问
		
	}
	
	
	
	


	

	
	/*  按页获取user ＋使用查询构造器查询＋获取多结果  （建议用）  */
	function get_userList2($openId,$limit, $offset){
		$this->db->select();
		$query = $this->db->get('j_user');
		$query = $this->db->get_where('j_user', array('openId' => $openId), $limit, $offset);
		$result = $query->result_array();
		return $result;
		
		/* 遍历取数据
		 * foreach ($query->result_array() as $row)
			{
			    echo $row['title'];
			    echo $row['name'];
			    echo $row['email'];
			}
		 */
	}
	
	
// 	function read($id){
// // 		$this->load->database ();
// 		//$query = $this->db->get('j_user',$id);
// 		$rs = $this->db->simple_query("select * from j_client where id =".$id);
// 		$result = $rs->fetchAll();
// 		return $result;//这里返回的是一个数组，可以通过$query['id'],$query['title']//进行访问
// 	}
	
// 	function base_insert(){
// 		$name = $this->input->post('name');
// 		$idcard = $this->input->post('idcard');
		
// 		$province = $this->input->post('province');
// 		$city = $this->input->post('city');
// 		$area = $this->input->post('area');
		
// 		$phones = $this->input->post(['phone']);
		
// 		return $phones;
// 		exit;
		
// 		$arr = array('name'=>$name,'idcard'=>$idcard,'province'=>$province,'city'=>$city,'area'=>$area);
		
// 		// 事务开始
// 		$this->db->trans_start();
		
// 		$this->db->insert('j_client',$arr);
// 		$id = $this->db->insert_id(); //获得刚刚新增记录的id
		
// 		// 事务结束
// 		$this->db->trans_complete();
		
// 	}
	
	//$id = $this->input->get('id');




}